package com.songshu.sb.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class LogAspect {

    // 使用 Spring 注入的 ObjectMapper，而不是自己创建
    @Autowired
    private ObjectMapper objectMapper;

    @Pointcut("execution(* com.songshu.sb.controller..*.*(..))")
    public void logPointcut() {
    }

    @Around("logPointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {

        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        // 请求信息
        log.info("【请求】URL: {}, 方法: {}, 参数: {}",
                request.getRequestURI(),
                request.getMethod(),
                Arrays.toString(joinPoint.getArgs()));

        long start = System.currentTimeMillis();

        Object result = joinPoint.proceed();

        log.info("【响应】耗时: {}ms, 返回: {}",
                System.currentTimeMillis() - start,
                objectMapper.writeValueAsString(result));

        return result;
    }
}